package com.mongodb.mongodbboot.service.impl;

import cn.hutool.core.date.DateBetween;
import com.mongodb.BasicDBObject;
import com.mongodb.client.MongoCursor;
import com.mongodb.mongodbboot.config.MongoShardingConfig;
import com.mongodb.mongodbboot.service.SampleQueryService;
import com.mongodb.mongodbboot.service.SampleStatusService;
import com.mongodb.mongodbboot.unitl.OnlineUtils;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author huyunqiang
 * @Description
 * @Date 2025/5/9 14:10
 **/
@Service
public class SampleStatusServiceImpl implements SampleStatusService {

    @Autowired
    SampleQueryService mongoQueryService;

    @Autowired
    private MongoShardingConfig mongoShardingConfig;

    @Override
    public void sampleStatus() {

        String dateTime="2025-04-20 00:00:00,2025-05-09 13:59:59";
        String[] dateTimeList = dateTime.split(",");
        LocalDateTime begin = LocalDateTime.parse(dateTimeList[0], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime end = LocalDateTime.parse(dateTimeList[1], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        //处理结束时间到23:59:59
        end = OnlineUtils.processEndTime("2", end);
        begin = OnlineUtils.processBeginTime("2", begin);
        List<Map<String, Object>> rowsList = new ArrayList<>();

        long startMillis = begin.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        long endMillis = end.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        List<BasicDBObject> orConditions = new ArrayList<>();
        // 添加多个组合条件
       /* orConditions.add(new BasicDBObject("mp_id", "ff80808188098e01018809b8c23900c13").append("mn", "0152316J0033813"));
        orConditions.add(new BasicDBObject("mp_id", "ff80808188098e01018809b8c23900c12").append("mn", "0152316J0033812"));
        BasicDBObject  param= new BasicDBObject("$or", orConditions);
        param.put("data_time", new BasicDBObject("$gte", new Date(startMillis)).append("$lte", new Date(endMillis)));
*/
        //多个条件组合查询嵌套查询
        orConditions.add(new BasicDBObject("mp_id", "ff80808188098e01018809b8c23900c1").append("values.standard_value", "51234"));
        orConditions.add(new BasicDBObject("mp_id", "ff80808188098e01018809b8c23900c12").append("values.standard_value", "5777771"));

        BasicDBObject param= new BasicDBObject("$or", orConditions);
        param.put("data_time", new BasicDBObject("$gte", new Date(startMillis)).append("$lte", new Date(endMillis)));

        // 设置投影，只获取mp_id和data_time字段
        Document projection = new Document("_id", 1)
                .append("mp_id", 1)
                .append("values.standard_value", 1);

        List<Document> result = new ArrayList<>();
        List<Document> data = new ArrayList<>();
        MongoCursor<Document> cursor = mongoShardingConfig.getHistoryMongoTemplate().getMongoDbFactory().getMongoDatabase("AMDB")
                .getCollection("sample_check_water").find(param).projection(projection).batchSize(10000)
                .iterator();
        while (cursor.hasNext()) {
            data.add(cursor.next());
        }
        result.addAll(data);

        System.out.println("---"+result.size());

    }
}
